package com.hrt.web.addressBook.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.AddressBook;
import com.hrt.web.addressBook.service.AddressBookService;
@Controller
@RequestMapping("HRTWEB/address")
public class AddressBookController extends BaseController<AddressBook>{
	@Autowired
	private AddressBookService addressBookService;
	/**
	 * 保存
	 * @param addressBook
	 * @param reponse
	 * @param request
	 */
	@RequestMapping("save")
	@ResponseBody
	public Map save(@ModelAttribute("addressInfor") AddressBook addressBook,HttpServletResponse reponse,
			HttpServletRequest request){
		String temp = request.getParameter("temp");
		Map result=new HashMap();
		if(temp!=null){
			//更改默认地址
			AddressBook defaultAddress = addressBookService.getDefaultAddress(addressBook.getMemberId(), 1);
			defaultAddress.setIsdefault(0);
			addressBookService.updateAddressBook(defaultAddress);
			
			addressBook.setIsdefault(1);
			
		}
		else{
			addressBook.setIsdefault(0);
		}
		
		addressBookService.saveAddressBook(addressBook);
		List<AddressBook> addressList = addressBookService.getAddListById(addressBook.getMemberId());
		result.put("addressList", addressList);
		return result;
	}
	/**
	 * 更新
	 * @param addressBook
	 * @param reponse
	 * @param request
	 */
	@RequestMapping("update")
	@ResponseBody
	public Map update(@ModelAttribute("addressInfor") AddressBook addressBook,HttpServletResponse response,
			HttpServletRequest request){
		Map result=new HashMap();
		if(addressBook!=null){
			addressBookService.updateAddressBook(addressBook);
			List<AddressBook> addressList = addressBookService.getAddListById(addressBook.getMemberId());
			result.put("addressList", addressList);
		}
		
		return result;
	}
	/**
	 * 设置默认地址
	 * @param addressBook
	 * @param reponse
	 * @param request
	 */
	@RequestMapping("setDefaultAddress")
	@ResponseBody
	public Map setDefaultAddress(@RequestParam(value = "addressId",required = true) Long addressId,
			@RequestParam(value = "memberId",required = true) Long memberId,
			HttpServletResponse response,HttpServletRequest request){
		Map result=new HashMap();
		//更改默认地址
		AddressBook defaultAddress = addressBookService.getDefaultAddress(memberId, 1);
		defaultAddress.setIsdefault(0);
		addressBookService.updateAddressBook(defaultAddress);
		//设置默认地址
		AddressBook address = addressBookService.getAddById(addressId);
		address.setIsdefault(1);
		addressBookService.updateAddressBook(address);
		
		//获取地址列表
		List<AddressBook> addressList = addressBookService.getAddListById(memberId);
		
		result.put("addressList", addressList);
		result.put("address", address);
		return result;
	}
	/**
	 * 删除
	 * @param addressId
	 * @param reponse
	 * @param request
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Long delete(@RequestParam(value = "addressId",required = true) Long addressId,
			@RequestParam(value = "memberId",required = true) Long memberId,
			HttpServletResponse reponse,
			HttpServletRequest request){
		addressBookService.deleteAddressBook(addressId);
		return memberId;
	}
	/**
	 * 显示
	 * @param memberId
	 * @param reponse
	 * @param request
	 */
	@RequestMapping("show")
	public ModelAndView show(@RequestParam(value = "memberId",required = true) Long memberId,
			HttpServletResponse reponse,HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		view.addObject("memberId", memberId);
		view.setViewName("/HRTWEB/address/show");
		return view;
	}
	/**
	 * 修改地址
	 * @param memberId
	 * @param reponse
	 * @param request
	 */
	@RequestMapping("edit")
	@ResponseBody
	public Map edit(@RequestParam(value = "memberId",required = true) Long memberId,
			@RequestParam(value = "addressId",required = true) Long addressId,
			HttpServletResponse reponse,HttpServletRequest request){
		Map map = new HashMap();
		if(addressId!=0){
			
			AddressBook address = addressBookService.getAddById(addressId);
			map.put("address", address);
		}
		
		return map;
	}
	/**
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("getAddress")
	@ResponseBody
	public Map getAddress(@RequestParam(value = "id",required = true) Long addressId,HttpServletRequest request){
		Map map = new HashMap();
		AddressBook addressBook = addressBookService.getAddById(addressId);
		map.put("addressBook", addressBook);
		return map;
	}
	
}
